import Card
__author__ = "Massimo Bonvicini"
__date__ = "$Jun 25, 2009 12:08:38 PM$"

import logging
import toolbox

log = logging.getLogger(__name__)

class Card(object):
    '''
    Creates a new playing card.
    '''
    showList = ['id', 'name', 'color']
    
    def __init__(self, name, color, edition, category, abilities):
        self.id = toolbox.genID(name)
        self.deck = None
        self.owner = None
        self.controller = None
        self.name = name
        self.color = color
        self.category = category
        self.abilities = abilities
        self.inplay = False
        self.edition = edition

    def __repr__(self):
        return toolbox.str_impl(self, self.showList)
    
    def __str__(self):
        return repr(self)
    
    def setDeck(self, deck):
        self.deck = deck
        
    def setOwner(self, owner):
        self.owner = owner
        
    def setController(self, controller):
        self.controller = controller
        
    def isInPlay(self):
        return self.inplay

    def play(self):
        if not self.isInPlay(): 
            self.inplay = True
            self.on_play()

class Spell(object):
    def __init__(self, cost):
        self.cost = cost

class Tappable(object):
    def __init__(self, tappable=True, tapped=False):
        self.tappable = tappable
        self.tapped = tapped
    
    def untap(self):
        if self.isTapped():
            try:
                self.on_untap
            except AttributeError:
                log.error('Please implement "on_untap" method')
            else:
                self.tapped = False
                self.on_untap()

    def tap(self):
        if self.isTappable():
            try:
                self.on_tap
            except AttributeError:
                log.error('Please implement "on_tap" method')
            else:
                self.tapped = True
                self.on_tap()

    def isTapped(self):
        return self.tapped

    def isTappable(self):
        return self.tappable and not self.tapped

class Land(Card, Tappable):
    def __init__(self, name, color, edition, category, abilities):
        Card.__init__(self, name, color, edition, category, abilities)
        Tappable.__init__(self)
        
    def on_tap(self):
        self.owner.manaPool.addMana(color=self.color, qty=1)
        
    def on_untap(self):
        pass

    def on_play(self):
        if not self.owner.land_played:
            self.owner.land_played = True
            return True
        else:
            return True

class Creature(Card, Spell, Tappable):
    def __init__(self, name, color, edition, category, abilities, cost, power, toughness):
        Card.__init__(self, name, color, edition, category, abilities)
        Spell.__init__(self, cost)
        Tappable.__init__(self)
        self.power  = power
        self.toughness = toughness

    def on_tap(self):
        pass

    def on_untap(self):
        pass

    def on_play(self):
        return self.owner.pay(self)
